package logic

import (
	"context"
	"fmt"
	"zerorpc/repository/gfzero/dao"

	"zerorpc/app/gfzero/rpc/internal/svc"
	"zerorpc/app/gfzero/rpc/types/login"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *login.LoginRequest) (*login.LoginResponse, error) {
	User, err := dao.User.Ctx(l.ctx).Where(dao.User.Columns().Username, in.Mobile).One()
	if err != nil {
		l.Errorf("数据库查询出错:%s", err.Error())
		return &login.LoginResponse{Message: "账号不存在"}, nil
	}
	if User["password"].String() != in.Password {
		return &login.LoginResponse{Message: "密码错误"}, nil
	}
	fmt.Println(User.Map())
	return &login.LoginResponse{
		Message: "登录成功",
	}, nil
}
